package com.team.arms.service_management_before.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.service_management_before.domain.Schedule;
import com.team.arms.service_management_before.service.ScheduleService;
import com.team.arms.yuding.domain.VoluntaryInformation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author yjh
 * @create 2022-06-24-14:59
 * @description:
 */
@Api( tags = "日程安排")
@RestController
@RequestMapping("/serviceManagementBefore/Schedule")
public class ScheduleController extends BaseController {

    @Autowired
    private ScheduleService scheduleService;

    /**
     * 查询日程安排表
     * @return
     */
    @ApiOperation("查询日程安排表")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Schedule> list = scheduleService.list();
        PageInfo<Schedule> pageInfo = new PageInfo<Schedule>(list);
        PageVO<Schedule> schedulePageVO = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, schedulePageVO);
        schedulePageVO.setResult(pageInfo.getList());
        schedulePageVO.setPage(pageNum);
        return AjaxResult.success(schedulePageVO);
    }


    /**
     * 根据id查询日程安排表
     * @param id
     * @return
     */
    @ApiOperation("根据id查询日程安排表")
    @GetMapping("/selectById")
    public AjaxResult selectById( @RequestParam(value = "schedule_id")String id){
        return AjaxResult.success(scheduleService.selectById(id));
    }

    public static String humpToUnderline(String str) {
        String regex = "([A-Z])";
        Matcher matcher = Pattern.compile(regex).matcher(str);
        while (matcher.find()) {
            String target = matcher.group();
            str = str.replaceAll(target, "_"+target.toLowerCase());
        }
        return str;
    }
    /**
     *
     * @param field
     * @param content
     * @return
     */
    @ApiOperation("根据对应字段查询日程安排表")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        field = humpToUnderline(field);
        return AjaxResult.success(scheduleService.selectByFiled(field,content));
    }

    /**
     * 新增日程安排表
     */
    @ApiOperation("新增日程安排表")
    @PostMapping("/install")
    public AjaxResult installSchedule(@RequestBody Schedule schedule){
        return toAjax(scheduleService.insertAll(schedule));
    }

    /**
     * 修改日程安排表
     * @param id
     * @param schedule
     * @return
     */
    @ApiOperation("修改日程安排表")
    @Log(title = "修改日程安排表", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateSchedule(
            @RequestParam(value = "schedule_id")String id,
            @RequestBody Schedule schedule){
        QueryWrapper<Schedule> wrapper = new QueryWrapper();
        wrapper.eq("schedule_id",id);
        Schedule byId = scheduleService.getById(id);
        return toAjax(scheduleService.update(schedule,wrapper));
    }

    /**
     * 删除日程安排表
     * @param id
     * @return
     */
    @ApiOperation("批量删除日程安排表")
    @DeleteMapping("/delete")
    public AjaxResult deleteSchedule(@RequestParam("clothing_id") List<String> id){
        return toAjax(scheduleService.deleteByClothingIds(id));
    }

    @ApiOperation("删除日程安排表")
    @DeleteMapping("/deleteById")
    public AjaxResult deleteById(@RequestParam("clothing_id") String id){
        return toAjax(scheduleService.deleteByClothingId(id));
    }

    /**
     * 导出日程安排表
     */
    @ApiOperation("导出日程安排表")
    @Log(title = "日程安排表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<Schedule> list = scheduleService.list();
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), VoluntaryInformation.class).sheet("日程安排表")
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("根据条件查询日程安排表")
    @PostMapping("/selectListSchedule")
    public TableDataInfo selectListSchedule(
            @RequestBody(required = false) Schedule schedule){

        startPage();

        List<Schedule> list = scheduleService.selectClothingTable(schedule);

        return getDataTable(list);
    }
}
